<template>
  <div class="box">
    <div class="pre-box">
      <h1>WELCOME</h1>
      <P>台球管理系统</P>
      <div class="pre-img">
        <div class="img-box"></div>
      </div>
    </div>
    <div class="refister">
      <div class="title-box">
        <h1>注册</h1>
      </div>
      <div class="input-box">
        <input type="text" placeholder="用户名" />
        <input type="password" placeholder="密码" />
        <input type="password" placeholder="确认密码" />
      </div>
    </div>

    <div class="login">
      <div class="title-box">
        <h1>登录</h1>
      </div>
      <el-form
        class="input-box"
        size="medium"
        :model="form"
        ref="LoginFormRef"
        :rules="loginFormRules"
      >
        <el-form-item prop="userName">
          <el-input
            prefix-icon="el-icon-s-custom"
            v-model="form.userName"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="form.password"
            type="password"
          ></el-input>
        </el-form-item>

        <el-form-item label-width="30px">
          <button type="primary" @click="login">登录</button>
          <button type="info" @click="reset()">重置</button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      background: {
        backgroundImage:
          "url(" + require("@/assets/img/微信图片_20231226192518.jpg") + ")",
      },
      form: {
        flag: true,
        userName: "",
        password: "",
      },
      loginFormRules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在3到10个字符之间",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 15,
            message: "长度在6到15个字符之间",
            trigger: "blur",
          },
        ],
      },
      token: "",
      flag: true,
      isShopowner: false,
    };
  },
  methods: {
    login() {
      //点击登录的时候先调用validate方法验证表单内容是否有误
      this.$refs.LoginFormRef.validate((valid) => {
        //如果valid参数为true则验证通过
        if (!valid) {
          return;
        }
        var params = new URLSearchParams();
        params.append("userName", this.form.userName);
        params.append("password", this.form.password);
        //发送请求进行登录
        this.$axios.post(`/LoginServlet`, params).then((res) => {
          let data = JSON.parse(res.data);
          console.log(data);
          if (!data.queryFlag) {
            return this.$message.error("登录失败:" + data.msg);
          }
          this.token = data.token;
          //保存token
          localStorage.setItem("token", res.headers.token);
          localStorage.setItem("Uid", data.Uid);
          localStorage.setItem("isShopowner", this.isShopowner);
          // 导航至/home
          this.$router.push("/home");
          window.location.reload();
          this.$message({
            type: "success",
            message: "登录成功!",
            duration: 2000, // 设置显示时间为2秒
          });
        });
      });
    },
    reset() {
      this.form.userName = "";
      this.form.password = "";
    },
    bubleCreate() {
      const body = document.body;
      const buble = document.createElement("span");
      buble.classList.add("balloon");
      let r = Math.random() * 5 + 25;
      buble.style.width = r + "px";
      buble.style.height = r + "px";
      buble.style.left = Math.random() * innerWidth + "px";
      body.append(buble);
      //每4秒清除一个
      setTimeout(() => {
        buble.remove();
      }, 4000);
    },
  },
  mounted() {
    setInterval(() => {
      this.bubleCreate();
    }, 600);
  },
};
</script>

<style scoped>
@import url(../../assets/css/LoginView.css);

.background {
  padding: 0px;
  margin: 0px;
  background-size: 100%;
  height: 100%;
  width: 100%;
}

.paginationBlock {
  margin-left: 90px;
}

.el-card .el-col {
  margin-right: 8px;
}

.allocationRoleDialog .el-dialog__body {
  padding: 15px 35px;
}
</style>
